/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.AdminModel;
import com.hykj.xxltest.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员
 *ddddd
 * @author innel
 * @email 2638086622@qq.com
 * @date 2018-02-27 10:51:25
 */
@Controller
@RequestMapping("/admin")
@Api(description = "管理员")
@ApiIgnore
public class AdminController {

    private static Logger logger = Logger.getLogger(AdminController.class);

    @Autowired
    private AdminService adminService;


//ddddddd


    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10", required = false)
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "", required = false)
                        @RequestParam(required = false)
                                Integer id,
                        @ApiParam(value = "状态 -1冻结 0正常 -10删除", required = false)
                        @RequestParam(required = false)
                                Integer status,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字", required = false)
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页", required = false)
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("id", id);
        params.put("status", status);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(adminService.getDList(params));
        }
        PageInfo<AdminModel> p = adminService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack info(
            @ApiParam(value = "当前资源的ID", required = true)
            @RequestParam Integer id) {
        AdminModel admin = adminService.getByIdD(id);
        if (admin == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(@ApiParam(value = "姓名", required = true)
                        @RequestParam
                                String realname,
                        @ApiParam(value = "登录名", required = true)
                        @RequestParam
                                String username,
                        @ApiParam(value = "密码", required = true)
                        @RequestParam
                                String password,
                        @ApiParam(value = "状态 -1冻结 0正常 -10删除", required = true)
                        @RequestParam
                                Integer status,
                        @ApiParam(value = "选填参数控制 默认不填", hidden = true)
                        @RequestParam(required = false)
                                Integer extra) {
        AdminModel admin = new AdminModel();
        admin.setRealname(realname);
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setStatus(status);

        adminService.create(admin);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "资源ID", required = true)
                          @RequestParam(required = true)

                                  Integer id,

                          @ApiParam(value = "姓名", required = false)
                          @RequestParam(required = false)

                                  String realname,
                          @ApiParam(value = "登录名", required = false)
                          @RequestParam(required = false)

                                  String username,
                          @ApiParam(value = "密码", required = false)
                          @RequestParam(required = false)

                                  String password,
                          @ApiParam(value = "状态 -1冻结 0正常 -10删除", required = false)
                          @RequestParam(required = false)

                                  Integer status,
                          @ApiParam(value = "选填参数控制 默认不填", required = false)
                          @RequestParam(required = false)
                                  Integer searchKey) {
        AdminModel admin = new AdminModel();
        admin.setId(id);
        admin.setRealname(realname);
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setStatus(status);
        adminService.update(admin);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除当前资源",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "资源ID", required = true)
                          @RequestParam(required = true)
                                  int id) {

        AdminModel newAdmin = new AdminModel();
        newAdmin.setStatus(-10);
        newAdmin.setId(id);
        adminService.update(newAdmin);
        return new AppBack();
    }

}
